package le.grand;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class ReservationEntry {

	private static final SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyy");

	private int numOfPassengers;
	private List<String> names = new ArrayList<>();
	private int numOfChildren;

	public ReservationEntry(int numOfPassengers, String name) {
		this.numOfPassengers = numOfPassengers;
		this.names.add(name);
	}

	public ReservationEntry(int numOfPassengers) {
		this.numOfPassengers = numOfPassengers;
	}

	public ReservationEntry() {
	}

	public int getNumOfChildren() {
		return numOfChildren;
	}

	public int getNumOfPassengers() {
		return numOfPassengers;
	}

	public void addName(String name) {
		// dont add wandering dates
		try {
			sdf.parse(name);
		} catch (ParseException e) {
			names.add(name);
			int countChildren = countChildren(name);
			numOfChildren += countChildren;
		}
	}

	private int countChildren(String s) {
		int k = 0;
		String[] splits = StringUtils.split(s, " ");
		for (String sp : splits) {
			if (sp.endsWith("/CH") || sp.endsWith("/CHD")) {
				k++;
			}
		}
		return k;
	}

	public void setNumOfPassengers(int numOfPassengers) {
		this.numOfPassengers = numOfPassengers;
	}

	public List<String> getNames() {
		return names;
	}

	@Override
	public String toString() {
		return "ReservationEntry [numOfPassengers=" + numOfPassengers + ", names=" + names + "]";
	}

}
